package com.rfid.afd.activity.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.rfid.db.bean.Machine;
import com.rfid.rls_psw.R;
import com.rfid.util.RfidManager;
import com.rfid.util.AnimatorHelper;
import com.rfid.util.DBManager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordActivity extends AppCompatActivity implements RfidManager.RfidListener {
    @BindView(R.id.epc)
    TextView mEpc;
    @BindView(R.id.edit)
    EditText mEditText;
    @BindView(R.id.number)
    EditText mNumber;
    @BindView(R.id.rssi)
    EditText mRssi;

    private String mEpcStr;

    public static void startAcitivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context,RecordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        ButterKnife.bind(this);

        mEditText.setEnabled(false);
        RfidManager.getInstance().setRfidListener(this);
        RfidManager.getInstance().setOutputPower(15);
        RfidManager.getInstance().start();
        mRssi.setText("" + 15);
        long count = DBManager.queryMachineCount();
        mNumber.setText("" + count);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        RfidManager.getInstance().setRfidListener(null);
        RfidManager.getInstance().stop();
    }

    @Override
    public void onOutputPower(int power) {
        mRssi.setText(power + "");
    }

    @Override
    public synchronized void onRfid(String rfid) {
        if(!TextUtils.isEmpty(mEpcStr) || TextUtils.isEmpty(rfid)) {
            return;
        }
        mEpcStr = rfid;
        RfidManager.getInstance().stop();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mEpc.setText("标签号:" + mEpcStr);
                mEditText.setEnabled(true);
            }
        });
    }

    public void rssiConfirm(View view) {
        String rssi = mRssi.getText().toString();
        if(TextUtils.isEmpty(rssi)) {
            return;
        }
        RfidManager.getInstance().setOutputPower(Integer.valueOf(rssi));

        RfidManager.getInstance().getOutputPower();
    }

    public void record(View view) {
        String info = mEditText.getText().toString();
        if(TextUtils.isEmpty(mEpcStr)) {
            Toast.makeText(this,"EPC未扫描到，请重新扫描",Toast.LENGTH_SHORT).show();
            AnimatorHelper.shake(this,mEpc);
            return;
        }
        if(TextUtils.isEmpty(info)) {
            Toast.makeText(this,"信息未填写，请填写信息",Toast.LENGTH_SHORT).show();
            AnimatorHelper.shake(this,mEditText);
            return;
        }
        Machine machine = new Machine();
        machine.setEpc(mEpcStr);
        String number = mNumber.getText().toString();
        if(!TextUtils.isEmpty(number)) {
            machine.setInfo(info + Integer.valueOf(number));
        } else {
            machine.setInfo(info);
        }
        long r = DBManager.insertMachine(machine);
        if(r < 0) {
            Toast.makeText(this,"请检查标签是否重复录入",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!TextUtils.isEmpty(number)) {
            int n = Integer.valueOf(number);
            n++;
            mNumber.setText("" + n);
        }
        mEpc.setText("录入成功，继续扫描");
        mEditText.setEnabled(false);
        mEpcStr = null;
        RfidManager.getInstance().start();
    }

    public void rescan(View view) {
        mEpc.setText("请扫描");
        mEditText.setEnabled(false);
        mEpcStr = null;
        RfidManager.getInstance().start();
    }
}